package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.CurrentDataJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import com.cicc.beergamevirtualsimulationsystem.model.parameter.dto.RoomParameterDto;
import com.cicc.beergamevirtualsimulationsystem.model.userType.RoleTypeParameterDto;
import com.cicc.beergamevirtualsimulationsystem.service.ParameterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class CurrentDataJsonMessageHandler implements JsonMessageHandler<CurrentDataJsonWebSocketMessage> {


    @Resource
    private ParameterService parameterService;
    @Override
    public void handle(WebSocketSession session, CurrentDataJsonWebSocketMessage message) {
        String roomId = session.getAttributes().get("roomId").toString();
        String userId = session.getAttributes().get("userId").toString();
        String roomInfo = null;
        try {
            roomInfo = RoomInfoManager.getRoomInfo(roomId, userId);
        } catch (Exception e) {

                RoomParameterDto roomParameterDto = parameterService.queryParameterByRoomId(roomId.toString());
                //获取对应角色的人数限制
                List<RoleTypeParameterDto> roleTypeParameterList = roomParameterDto.getRoleTypeParameterList();

                Map<String, Integer> collect = roleTypeParameterList.stream().collect(Collectors.toMap(
                        entity -> String.valueOf(entity.getRoleType()), // key 是 String 类型的 roleType
                        RoleTypeParameterDto::getRoleNum                   // value 是 roleNum
                ));

                //角色类型id和房间映射
                Map<String, String> mapping = roleTypeParameterList.stream().collect(Collectors.toMap(entity -> String.valueOf(entity.getRoleType()), RoleTypeParameterDto::getId));

                //System.out.println("映射--------------"+mapping);

                RoomInfoManager.initializeRoom(roomId.toString(), roomParameterDto.getCurrentRoomParameter().getGameTime(), roomParameterDto.getCurrentRoomParameter().getCycles(), collect,mapping,userId);
        }
        try {
            roomInfo= RoomInfoManager.getRoomInfo(roomId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        WebSocketMessageSender.send(session, new CurrentDataJsonWebSocketMessage(roomInfo));
    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.GET_CURRENT_DATA.getValue();
    }

    @Override
    public Class<CurrentDataJsonWebSocketMessage> getMessageClass() {
        return CurrentDataJsonWebSocketMessage.class;
    }
}
